package com.apes.fn.server.pickUpCar.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.*;

/**
 * @Classname PickUpCar
 * @Description 接车单
 * @Date 2020/1/14 15:06
 * @Created by wsj
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PickUpCar.CONST_TABLE_NAME,indexes = {
        @Index(columnList = "fn_customer_expand_id")
})
public class PickUpCar extends Model {
    public static final String CONST_TABLE_NAME = "fn_pick_up_car";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "接车单", padding = 5 , prefix = "JC%(company)%(yy)%(month)",useDateRange=true)
    @Comment("接车单号")
    private String id;

    @Column(name = "note", length = 400)
    @Comment("备注")
    private String note;

    @Column(name = "fault_description", length = 120)
    @Comment("故障描述")
    private String faultDescription;

    @Column(name = "phone_number", length = 32)
    @Comment("电话号码")
    private String phoneNumber;

    @Column(name = "customer_name", length = 32)
    @Comment("客户姓名")
    private String customerName;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fn_customer_expand_id", referencedColumnName = "id")
    @Comment("赋能客户车辆扩展信息")
    private FnCustomerExpand fnCustomerExpand;

    @Column(name = "license_number", length = 32)
    @Comment("车牌号码")
    private String licenseNumber;

    @Column(name = "mileage", length = 32)
    @Comment("进店里程数")
    private String mileage;

    @Column(name = "source_business_type", length = 32, nullable = true)
    @Comment("来源单据类型")
    private String sourceBusinessType;

    @Column(name = "source", length = 30)
    @Comment("来源单据号码")
    private String source;



    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    @Comment("门店")
    private Dept dept;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parking_uid", referencedColumnName = "id")
    @Comment("接车人员")
    private PartyRole parkingUid;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;


    @Comment("油表数")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fuel_meter", nullable = false, referencedColumnName = "id")
    private Enumeration fuelMeter;

   
    @Comment("接车类型")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "pick_car_type", nullable = false, referencedColumnName = "id")
    private Enumeration pickCarType;

    @States(name = "单据状态", values = {
            @State(id = "approve", name = "审核"),
            @State(id = "finish", name = "完成"),
            @State(id = "repeal", name = "终止")
    })
    @Column(name = "state", length = 10, nullable = false)
    private String state ="approve";

    @Column(name = "notice_pick_car",nullable = false,columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("通知取车")
    private boolean noticePickCar = false;

    @Column(name = "external",nullable = false,columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("外部定损")
    private boolean external = false;

    @Column(name = "advance_pick_car",nullable = false,columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("提前取车")
    private boolean advancePickCar = false;

    @Column(name = "parking_lot",nullable = false,columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("转停车场")
    private boolean parkingLot = false;

    @Column(name = "key_number", length = 3)
    @Comment("钥匙号")
    private int keyNumber;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "repeal_uid", referencedColumnName = "id")
    @Comment("终止人")
    private PartyRole repealUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "repeal_date")
    @Comment("终止日期")
    private Date repealDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "finish_uid", referencedColumnName = "id")
    @Comment("完成人")
    private PartyRole finishUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "finish_date")
    @Comment("完成日期")
    private Date finishDate;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "estimate_date")
    @Comment("预计交车日期")
    private Date estimateDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "notice_uid", referencedColumnName = "id")
    @Comment("通知取车人")
    private PartyRole noticeUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "notice_date")
    @Comment("通知取车日期")
    private Date noticeDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "advance_uid", referencedColumnName = "id")
    @Comment("提前交车人")
    private PartyRole advanceUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "advance_date")
    @Comment("提前交车日期")
    private Date advanceDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parking_lot_uid", referencedColumnName = "id")
    @Comment("转停车场人")
    private PartyRole parkingLotUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "parking_lot_date")
    @Comment("转停车场日期")
    private Date parkingLotDate;

    @Column(name ="advance_remark", columnDefinition = "CLOB")
    @Comment("未结算交车原因")
    private String advanceRemark;

    @Column(name ="risk_Car", columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("定损风险车")
    private boolean riskCar = false;

    /**
    待检测 , 未检测, 已完成,暂存,已关闭
     */
    @Column(name ="detection_Status")
    @Comment("36项检测状态")
    private String detectionStatus;

    @OneToMany(mappedBy = "pickUpCar", cascade = CascadeType.ALL, orphanRemoval = true ,fetch=FetchType.EAGER )
    private Set<PickUpCarItem> pickUpCarItem = new HashSet();

    public Set<PickUpCarItem> getPickUpCarItem() {
        if( pickUpCarItem == null ) return new HashSet();
        for (PickUpCarItem item : pickUpCarItem) {
            item.setPickUpCar(this);
        }
        return pickUpCarItem;
    }

    @OneToMany(mappedBy = "pickUpCar", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<PickUpType> pickUpTypes = new ArrayList();

    public List<PickUpType> getPickUpTypes() {
        if(pickUpTypes == null) return new  ArrayList();
        for (PickUpType item : pickUpTypes) {
            if("T1".equals(item.getPickUpType().getId()) && (item.getRemarks() == null || "".equals(item.getRemarks().trim()))){
                throw new RuntimeException("接车类型为其他，必须填写备注信息");
            }
            item.setPickUpCar(this);
        }
        return pickUpTypes;
    }


    public void setPickUpCarItem(Set<PickUpCarItem> pickUpCarItem){
        this.pickUpCarItem.clear();
        if (pickUpCarItem != null) {
            this.pickUpCarItem.addAll(pickUpCarItem);
        }
    }
}
